import {
  createRouter,
  createWebHashHistory
} from "vue-router";
import useSettingsStore from "@/store/modules/settings";

let routes = [{
    path: "/",
    redirect: "/login"
  },
  {
    path: "/login",
    name: "Login",
    meta: {
      title: "登录"
    },
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/index",
    name: "Index",
    meta: {
      title: "首页"
    },
    component: () => import("@/views/AmenuIndex/index.vue"),
  },
  {
    path: "/BdataPage",
    name: "BdataPage",
    meta: {
      title: "数据"
    },
    component: () => import("@/views/BdataPage/index.vue"),
  },
  {
    path: "/Cworkbench",
    name: "Cworkbench",
    meta: {
      title: "工作台"
    },
    component: () => import("@/views/Cworkbench/index.vue"),
  },
  {
    path: "/Dmy",
    name: "Dmy",
    meta: {
      title: "我的"
    },
    component: () => import("@/views/Dmy/index.vue"),
  },
  {
    path: "/DangerousProjectDetail",
    name: "DangerousProjectDetail",
    meta: {
      title: "危大工程管理流程"
    },
    component: () => import("@/views/Cworkbench/dangerousProject/details.vue"),
  },
  {
    path: "/BDangerousProjectDetail",
    name: "BDangerousProjectDetail",
    meta: {
      title: "危大工程管理流程"
    },
    component: () => import("@/views/BdataPage/ERiskProject/details.vue"),
  },
  {
    path: "/WarningDetail",
    name: "WarningDetail",
    meta: {
      title: "预警处理管理流程"
    },
    component: () => import("@/views/Cworkbench/excavation/details.vue"),
  },
  {
    // 配置404页面
    path: "/:catchAll(.*)",
    name: "NotFound",
    component: () => import("@/views/exception/NotFound.vue"),
  },
];
// 路由
const router = createRouter({
  history: createWebHashHistory(),
  routes,
  // scrollBehavior: () => ({ left: 0, top: 0 }),
  scrollBehavior(to, from, savePosition) {
    // console.log('savePosition:', savePosition);
    if (savePosition) {
      // 解决页面从列表页跳转到详情页返回,初始在原来位置
      return savePosition;
    } else {
      // 解决页面跳转后页面高度和前一个页面高度一样
      return {
        left: 0,
        top: 0
      };
    }
  },
});

// 导航守卫
router.beforeEach((to, from, next) => {
  to.meta.title && useSettingsStore().setTitle(to.meta.title);
  next();
});

// 导出
export default router;